package com.neu.neumall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.neu.neumall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id = "2021000121624978";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCTQAd/5qklco8njXFhqWYaXyrV6Ffa8IEmdshaZzsRkWJOPkHnLV6iSIjCkkbHWwg0oGlkF80MG29s4hn3zzjU87I725/Ux2Gk6YrcLw04MrpajFoOO91caIp2vQQIb7VeY9bKjdCavsPyBgK+Ce1d5ltZ21oGoxaGm+69b5OHdXJ/+ti+2jYKi619nTwbhCjzBBMyXidyUdOsizktSp1QnIz/RzP/TvNndYcwH6Ax4O8Rkr5YGpxlDIGg41DwGTJ3oTDuSW1MpCvIWj3NglVqmrNFs4NCvqJMxktei9tpJWYHxkAVFo3j7Lo1a4J9eSg8ewIXkxHLJ+PrYENGO+VRAgMBAAECggEAE1YPJg6GG7LFda4vO2HhVIk2uMw/7TIz43cprCVuLkpnHFs0kET27lyUCOjofnlHxJFkBY7SkEpyAVxXiKEfl2dA+ndGr8xW2C8K2wsas0yGCCnyEXhvAxl/RURg6TKyIGj/5a1lvUxbssZzSb1H/FKpoE0br09vP9HuMdYbC2h/s3N8ME4MMLbEQAMS1+Mg23lk5heIDvnEOYKRBWh1+9LWaZyivO4SAhukcEYebUYlpJMl+flD2GbXPHOUhc2Z67KuQrkaD28XrvIQHkgD1ctdsEyWydQgQ+rnSKaIRAQmZeaWuIYPe1/M63ginFu/hjNrJNRppNexZ6BphTyeGQKBgQDRaFZWkA2qEaL1ycdW6zY6vPANTm1+MuvYs5dbTJkOThfYUGYFhZmuYwowMcXJnjQGK4pxcx5nbkJp944+4lXhSZJbtUQ5alYftbeeTOr3jg1yMNQYTv7YF1SIaqSApKj/D3URsTjZOi/6ZdzSMCZ5rEEDY7F2difBpaPIn1TJIwKBgQC0A0K2TiUeH/e9oO/PFotsrR3ygsZMIBwsQSdugcHv+b+Y4ue3JQZkAgzJ4LTm1iwFE7MmIYMz5y6HLE/8Dz8hi+usbZJqqVXd9twApQ6HRkuWww+icnFf88nV4omBmbKl2lzSy/TJ+n28remIXgeqCxyf7B2fGi5pkNKEXB2Q+wKBgA4slct+LK6aSwGACvIjMaSALW95I4grQjOE1HXllWaWep61snIlPOXE5mekttKS1sqkH9bWB/TQTcaGbyZqg42gQTvb5SZNwE6dKjSY2D6xas+T5odOsYlG6v3g2gmrG5VIIpB0AnkRqzWzt2Kyx6fstOnZ0aVHEiHVaGSoBEEpAoGAdeuI4QmR0QkZ7dOmJB9dmARTkNBP/AAw1GCd5wsDMyOEei9xoQyVAiBBpmEXv/tefLjp6zmay/5OAWhqwPBv55bT/HEOr+ZbhHq4pYd6W+0ViR4geMbFNBjsB7tNXmODnpfLpJ7MrR71DQtT5uvJ039R8gTnZpjs/GRNMz1wdM8CgYBGlZyizEhP37vcmPbm8gYOoRMoafNU9FBtUgt9caDRu9dtScRyZsxd26jegMrvBq5Pyh39uXC/IUGnpURRxTICKcTXPrc9gFYGFUItzwu3BkT6DACn4uML9Sc8Vqei3SCVvuWzQH28cBrG+VEeMTQ2mQxdo7rkD57qSvqbI4VKoQ==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhAPue2edGuLw5cF3yl7p8qpSLcUkeHQY7pbR6fRErMd43zVz4CWy9erOF1NQVyAIBmfKWfv5TwyHU6BSuiZH9lV0DU9Sx9/Hny8YcffA1qJESUVEAXcM/h+AVLzWtyKbV5urWtHqNf0YUQ43cmZQVGY2SdUXa2k0OOnrfcuNuh7tjp3Y03PjKoaEx6B6lR+7u4I2Y3Ummet6lrOuWya6RpmXVdYr1ZpsT7v5AiOFeRELwyr3pJ+KuxNSxOG/ti2vTqPyorcLJmS79evG+3qNpgDAijR5wl5O7kZ6aZbpvgpMprrJV6uT0Ht7pNH4+14TPkxCl8uu/3UD0REf0SJwRQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url = "http://f3jzxw.natappfree.cc/payed/notify";
//    http://vi8zn7.natappfree.cc/alipay_trade_page_pay_JAVA_UTF_8_Web_exploded/notify_url.jsp
    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url = "http://member.neumall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
